package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleUserAddDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleUserListDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleUserAddVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleUserListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.saas.domain.entity.DingBundleUserEntity;
import com.xbongbong.saas.model.DingBundleUserModel;
import com.xbongbong.saas.service.DingBundleUserService;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:13 下午
 */
@Service("dingBundleUserService")
public class DingBundleUserServiceImpl implements DingBundleUserService {
    
    private static  final Logger LOG = LoggerFactory.getLogger(DingBundleUserServiceImpl.class);

    @Resource
    private DingBundleUserModel dingBundleUserModel;
    @Resource
    private UserModel userModel;
    
    @Override
    public BundleUserListVO list(BundleUserListDTO bundleUserListDTO) throws XbbException {
        BundleUserListVO bundleUserListVO = new BundleUserListVO();
        try {
            Long bundleId = bundleUserListDTO.getDataId();
            String corpid = bundleUserListDTO.getCorpid();
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put("orderByStr","identity asc");
            params.put("bundleId",bundleId);
            params.put("corpid",corpid);
            params.put("del",0);
            List<DingBundleUserEntity> dingBundleUserModelEntityList = dingBundleUserModel.findEntitys(params);
            if ( bundleUserListDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.SALE_BUNDLE_MEMBER_ADD.getAlias()) ) {
                bundleUserListVO.setAddMemberPermission(true);
                bundleUserListVO.setRemoveMemberPermission(true);
            }
            bundleUserListVO.setList(dingBundleUserModelEntityList);
        } catch (Exception e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001,SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return bundleUserListVO;
    }
    
    @Override
    public BundleUserAddVO add(BundleUserAddDTO bundleUserAddDTO) throws XbbException {
        return null;
    }

    @Override
    public UserVO getMasterInfo(BaseDTO baseDTO) throws XbbException {
        UserEntity userEntity  = userModel.getByKey(baseDTO.getUserId(), baseDTO.getCorpid());
        //用户不存在
        if(userEntity == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.USER_NOT_EXIST);
        }
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(userEntity, userVO);
        return userVO;
    }
}
